<?php

/**
* alltosun.com 购物车widget cart.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-9-4 下午3:21:56 $
* $Id$
*/

class cart_widget
{
    public function get_list($param = array())
    {
        $is_all = 0;
        if (isset($param['is_all']) && $param['is_all']) {
            $is_all = 1;
        }

        $cart_list = cart_helper::cart_merge();
        if (!$cart_list) {
            return array();
        }
        $list = array();
        foreach ($cart_list as $k=>$v) {
            if ($v['is_gift'] == 0 && $is_all == 0) {
                $list[] = cart_helper::get_cart_info($v['sku_id'], $v['goods_num'], $v['is_promotion'], $v['is_gift']);
            }

            if ($is_all == 1) {
                $list[] = cart_helper::get_cart_info($v['sku_id'], $v['goods_num'], $v['is_promotion'], $v['is_gift'], $is_all);
            }
        }

        return $list;
    }

    public function add_cart($params = array())
    {
        $sku_id         = 0; // skuID
        $goods_num      = 1; // 商品数量
        $is_promotion   = 0; // 是否加价购
        $is_gift        = 0; // 是否为赠品
        $is_return_list = 0;
        $user_id        = user_helper::get_user_id();

        if (isset($params['sku_id']) && $params['sku_id']) {
            $sku_id = $params['sku_id'];
        }
        if (isset($params['goods_num']) && $params['goods_num']) {
            $goods_num = $params['goods_num'];
        }
        if (isset($params['is_promotion']) && $params['is_promotion']) {
            $is_promotion = $params['is_promotion'];
        }
        if (isset($params['is_gift']) && $params['is_gift']) {
            $is_gift = $params['is_gift'];
        }
        if (isset($params['is_return_list']) && $params['is_return_list']) {
            $is_return_list = $params['is_return_list'];
        }

        if (!$sku_id) {
            return '请选择要操作的商品';
        }

        if ($goods_num <= 0) {
            return '商品数量不能小于等于0';
        }

        // 通过sku_id 查询 商品的详细信息
        $sku_info = _uri('sku', array('id'=>$sku_id));
        $add_cart_list[] = array(
                'goods_id'      => $sku_info['goods_id'],
                'sku_id'        => $sku_info['id'],
                'goods_num'     => $goods_num,
                'is_promotion'  => $is_promotion,
                'is_gift'       => $is_gift,
                'parent_id'     => 0,
                'parent_sku_id' => 0
            );
        $goods_info = _uri('goods', $sku_info['goods_id']);
        if (!$goods_info || !$goods_info['is_sale']) { // 当前商品以下架
            return '当前商品以下架';
        }

        // liw add 2015-04-24 直接购买的不能加入购物车
        if($goods_info['is_direct_buy'] == 1) {
            return "此商品不能加入购物车，请直接下单购买";
        }

        // 查询是否具有赠品
        if ($is_promotion == 0 && $is_gift == 0) {  //如果购物车内没有当前商品 才查询是否具有赠品
            $gift_list = _model('goods_gift_relation')->getList(array('goods_id'=>$sku_info['goods_id'], 'is_multiple'=>1));

            $gift_ids = array();
            if ($gift_list) {
                foreach ($gift_list as $k=>$v) {
                    if (in_array($v['gift_id'], $gift_ids)) {
                        continue;
                    }

                    $goods_info = _uri('goods', $v['gift_id']);
                    if (!$goods_info['is_sale']) { // 当前商品以下架
                        continue;
                    }

                    $gift_ids[] = $v['gift_id'];
                    $add_cart_list[] = array(
                            'goods_id'      => $v['gift_id'],
                            'sku_id'        => $v['gift_sku_id'],
                            'goods_num'     => $v['gift_num'],
                            'is_promotion'  => 0,
                            'is_gift'       => 1,
                            'parent_id'     => $sku_info['goods_id'],
                            'parent_sku_id' => $sku_info['id']
                        );
                }
            }
        }

        if (!$user_id) {
            $cart_list = Cookie::get('cart');
            $cart_list = unserialize($cart_list);
        }

        foreach ($add_cart_list as $add_key => $add_value) {
            // 判断是否是加价购
            if ($add_value['is_promotion'] == 1) {
                $add_value['goods_num'] = 1;
            }

            // 判断库存 如果库存大于50不判断
            //if ($sku_info['stock'] < 50 && $sku_info['stock'] < $cart_info['goods_num']) {
            $sku_info = _uri('sku', $add_value['sku_id']);
            if (!$sku_info) {
                continue;
            }
            if ($sku_info['stock'] < $add_value['goods_num']) {
                if ($sku_info['stock'] < 1 && $add_value['is_gift'] == 0) {
                    return '当前商品没有库存了';
                } else if ($sku_info['stock'] < 1) {
                    continue;
                }
                $add_value['goods_num'] = $sku_info['stock'];
            }

            if ($add_value['goods_num'] > 200) {
                $add_value['goods_num'] = 200;
            }

            if ($user_id > 0) {
                $add_value['user_id'] = $user_id;
                $filter = array(
                    'user_id'       => $user_id,
                    'sku_id'        => $add_value['sku_id'],
                    'is_promotion'  => $add_value['is_promotion'],
                    'is_gift'       => $add_value['is_gift'],
                    'parent_sku_id' => $add_value['parent_sku_id'],
                );

                if ($cart_online_info = _uri('cart', $filter)) {
                    if ($is_promotion == 1) {
                        $cart_online_info['goods_num'] = 1;
                    } else {
                        $cart_online_info['goods_num'] += $add_value['goods_num'];
                        if ($cart_online_info['goods_num'] > $sku_info['stock']) {
                            $cart_online_info['goods_num'] = $sku_info['stock'];
                        }
                        if ($sku_info['stock'] < 1) {
                            $cart_online_info['goods_num'] = 1;
                        }
                    }

                    // 是否返回列表
                    if ($is_return_list == 1) {
                        $add_cart_list[$add_key]['goods_num'] = $cart_online_info['goods_num'];
                    }

                    _model('cart')->update($filter, $cart_online_info);
                } else {
                    _model('cart')->create($add_value);
                }
            } else {
                if (empty($cart_list)) {
                    $cart_list[] = $add_value;
                } else {
                    $in_cart = 0;
                    foreach ($cart_list as $k=>$v) {
                        if (!($v['sku_id'] == $add_value['sku_id'] && $v['is_promotion'] == $add_value['is_promotion'] && $v['is_gift'] == $add_value['is_gift'] && $v['parent_sku_id'] == $add_value['parent_sku_id'])) {
                            continue;
                        }

                        if ($add_value['is_promotion'] > 0) {
                            $cart_list[$k]['goods_num'] = 1;
                        } else {
                            $cart_list[$k]['goods_num'] += $add_value['goods_num'];

                            //if ($sku_info['stock'] < 50 && $cart_list[$k]['goods_num'] > $sku_info['stock']) {
                            if ($cart_list[$k]['goods_num'] > $sku_info['stock']) {
                                $cart_list[$k]['goods_num'] = $sku_info['stock'];
                            }
                            if ($sku_info['stock'] < 1) {
                                $cart_list[$k]['goods_num'] = 1;
                            }
                        }
                        $in_cart = 1;

                        // 是否返回列表
                        if ($is_return_list == 1) {
                            $add_cart_list[$add_key]['goods_num'] = $cart_list[$k]['goods_num'];
                        }

                        break;
                    }

                    if ($in_cart == 0) {
                        $cart_list[] = $add_value;
                    }
                }
            }
        }

        if (isset($cart_list) && $cart_list) {
            $cart_list = serialize($cart_list);
            Cookie::set_path(Config::get('cookie_path'));
            Cookie::set_domain(Config::get('cookie_domain'));
            Cookie::set('cart', $cart_list, time()+7*24*3600);
        }

        // 是否返回列表
        if ($is_return_list == 1) {
            return array('info'=> 'ok', 'list'=>$add_cart_list);
        }

        return true;
    }
}
?>